package org.gitos.cloud.server.mq.logic;


import org.gitos.cloud.domain.comm.Constant;
import org.gitos.cloud.domain.device.Device;
import org.gitos.cloud.domain.device.Router;
import org.gitos.cloud.domain.mq.MqMsg;
import org.gitos.cloud.domain.server.Led;
import org.gitos.cloud.domain.tcp.Bucket;
import org.gitos.cloud.domain.tcp.DeviceId;
import org.gitos.cloud.server.mq.PushManager;
import org.gitos.cloud.server.service.PushToAppService;
import org.gitos.cloud.server.service.RelationService;
import org.gitos.cloud.util.HexUtil;

/**
 * Created by szc on 2015/12/4.
 */
public class DeviceInfoHandler implements Runnable {
    private PushManager pushManager;
    private String channleId;
    private String nodeId;
    private MqMsg msg;
    private RelationService relationService;
    private Router router;
    private PushToAppService pushToAppService;

    public DeviceInfoHandler(MqMsg msg, Router router, PushToAppService pushToAppService) {
        this.msg = msg;
        this.router = router;
        this.pushToAppService = pushToAppService;
    }

    public MqMsg getMsg() {
        return msg;
    }

    public void setMsg(MqMsg msg) {
        this.msg = msg;
    }

    public PushManager getPushManager() {
        return pushManager;
    }

    public void setPushManager(PushManager pushManager) {
        this.pushManager = pushManager;
    }

    public String getChannleId() {
        return channleId;
    }

    public void setChannleId(String channleId) {
        this.channleId = channleId;
    }

    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public RelationService getRelationService() {
        return relationService;
    }

    public void setRelationService(RelationService relationService) {
        this.relationService = relationService;
    }

    @Override
    public void run() {
        MqMsg resultMsg = new MqMsg();
        resultMsg.setChannelId(channleId);
        resultMsg.setNodeId(nodeId);
        Bucket bucket = new Bucket();
        bucket.setCmd(Constant.CMD_DOWN_ALL_RESULT);
        Led led = null;
        try {
            led = new Led(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (relationService.saveLed(led)) {
            bucket.setContent(new byte[]{0x01});
            resultMsg.setBucket(bucket);
            Device device = new Device();
            device.setChannelId(msg.getChannelId());
            device.setNodeId(msg.getNodeId());
            DeviceId deviceId = msg.getBucket().getDeviceId();
            device.setDeviceId(deviceId);
            device.setLogin(true);
            router.registerDevice(device);
            pushToAppService.push(HexUtil.encodeHexStr(deviceId.getValue()),PushToAppService.OPEN);
        } else {
            bucket.setContent(new byte[]{0x00});
        }

        bucket.setDeviceId(msg.getBucket().getDeviceId());

        pushManager.sendMsg(resultMsg.getNodeId(), resultMsg);
    }
}
